home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / examples / insight / plugins / mybias.pro < prev    next >
Text File  |  1997-07-08  |  12KB  |  353 lines

  1. ; $Id: mybias.pro,v 1.15 1997/04/22 17:12:33 rob Exp $
  2. ;
  3. ; Copyright (c) 1997, Research Systems, Inc.  All rights reserved.
  4. ;   Unauthorized reproduction prohibited.
  5. ;+
  6. ; FILE:
  7. ;       mybias.pro
  8. ;
  9. ; PURPOSE:
  10. ;       This file contains an example Analysis PlugIn that does biasing.
  11. ;
  12. ; CONTENTS:
  13. ;       GENERAL ROUTINES
  14. ;           pro HandleEventsMyBias      - handle dialog box events
  15. ;
  16. ;       CALLBACK ROUTINES
  17. ;           fun ApplyMyBias             - Apply/OK entry point
  18. ;           fun PromptUserMyBias        - main entry point (creates dialog)
  19. ;
  20. ;       REGISTRATION FUNCTION
  21. ;           fun MyBias                  - registers the PlugIn
  22. ;
  23. ;-
  24.  
  25. ; *****************************************************************************
  26. ;       GENERAL ROUTINES
  27. ; *****************************************************************************
  28.  
  29. ; -----------------------------------------------------------------------------
  30. ;
  31. ;   Purpose:  Handle dialog events.
  32. ;
  33. pro HandleEventsMyBias, sEvent
  34.  
  35.     ;  Widget state information.
  36.     ;
  37.     common MyBiasCommon, psState
  38.     wGroup = (*psState).wMainBase
  39.  
  40.     ;  Catch errors.
  41.     ;
  42.     CATCH, error
  43.     if (error ne 0) then begin
  44.         CATCH, /CANCEL
  45.         void = DIALOG_MESSAGE(!ERR_STRING, DIALOG_PARENT=wGroup)
  46.         RETURN
  47.     endif
  48.  
  49.     ; ========================
  50.     ;     PROCESS EVENTS
  51.     ; ========================
  52.  
  53.     case (sEvent.id) of
  54.  
  55.         ; --------------------------------------
  56.         ;     Input text
  57.         ; --------------------------------------
  58.         (*psState).wInputText: begin
  59.  
  60.             ; (nothing to do now)
  61.  
  62.         end
  63.  
  64.         ; --------------------------------------
  65.         ;     Input browse button
  66.         ; --------------------------------------
  67.         (*psState).wInputBrowseButton: begin
  68.  
  69.             ;  Let user browser for a data name.
  70.             ;
  71.             void = INSGET( $
  72.                 NAME=inputName, $           ; returned name of data selected
  73.                 /EXCLUSIVE, $               ; only one selection
  74.                 TITLE='Select a vector.', $ ; title of browser
  75.                 DIMS_LIST=1, $              ; show 1D data only
  76.                 COUNT=count, $              ; returned count of items selected
  77.                 GROUP=wGroup, $             ; wiget group leader
  78.                 _EXTRA=(*psState).extra)    ; extra information
  79.  
  80.             ;  If user selected an item, set data name in text widget.
  81.             ;
  82.             if (count eq 1) then $
  83.                 WIDGET_CONTROL, (*psState).wInputText, SET_VALUE=inputName
  84.  
  85.         end
  86.  
  87.         ; --------------------------------------
  88.         ;     Bias bgroup
  89.         ; --------------------------------------
  90.         (*psState).wBiasBgroup: begin
  91.  
  92.             WIDGET_CONTROL, (*psState).wBiasBgroup, GET_VALUE=value
  93.             if (value eq 0) then $
  94.                 (*psState).biasMult = -1 $
  95.             else if (value eq 1) then $
  96.                 (*psState).biasMult = 1 $
  97.             else $
  98.                 (*psState).biasMult = 0
  99.  
  100.         end
  101.  
  102.         ; --------------------------------------
  103.         ;     OK/Apply/Cancel buttons
  104.         ; --------------------------------------
  105.         (*psState).wOKApplyCancelButtons: begin
  106.  
  107.             ;  Destroy dialog on successful OK selection, or if user canceled.
  108.             ;
  109.             if ((sEvent.type eq 'OK') or $
  110.                 (sEvent.type eq 'Cancel')) then $
  111.                 WIDGET_CONTROL, (*psState).wMainBase, /DESTROY
  112.         end
  113.  
  114.         ; --------------------------------------
  115.         ;     other events
  116.         ; --------------------------------------
  117.         else:   ; (do nothing)
  118.  
  119.     endcase
  120.  
  121. end                 ; HandleEventsMyBias
  122.  
  123. ; *****************************************************************************
  124. ;       CALLBACK ROUTINES
  125. ; *****************************************************************************
  126.  
  127. ; -----------------------------------------------------------------------------
  128. ;
  129. ;   Purpose:  Get data and bias it (called when Apply or OK button hit).
  130. ;             Fuction returns 1B on success, else 0B.
  131. ;
  132. function ApplyMyBias, $
  133.     CIDs=CIDs, $            ; OUT: command ID list from INSPUT/INSVIS calls
  134.     DATA_NAME=dataName, $   ; IN: name of data selected in visualization window
  135.     _EXTRA=extra            ; IN: information to pass to commands
  136.  
  137.     ;  Widget state information.
  138.     ;
  139.     common MyBiasCommon, psState
  140.     wGroup = (*psState).wMainBase
  141.  
  142.     ; ---------------------------------------------------------
  143.     ;  Catch errors.
  144.     ; ---------------------------------------------------------
  145.  
  146.     CATCH, error
  147.     if (error ne 0) then begin
  148.         CATCH, /CANCEL
  149.         void = DIALOG_MESSAGE(!ERR_STRING, DIALOG_PARENT=(*psState).wMainBase)
  150.         RETURN, 0B
  151.     endif
  152.  
  153.     ; ---------------------------------------------------------
  154.     ;  Check inputs.
  155.     ; ---------------------------------------------------------
  156.  
  157.     ;  Get and check input data name.
  158.     ;
  159.     WIDGET_CONTROL, (*psState).wInputText, GET_VALUE=inputName
  160.     inputName = inputName[0]
  161.     if (inputName eq '') then $
  162.         MESSAGE, 'Must specify input data.'
  163.  
  164.     ;  Get input data.
  165.     ;
  166.     inputData = INSGET( $
  167.         inputName, $                ; name of data to get
  168.         PTR_OUT=0, $                ; return data, thus ASSOC data will work
  169.         COUNT=count, $              ; returned number of items found
  170.         DIMS_LIST=1, $              ; data should have this dimensionality
  171.         GROUP=wGroup, $             ; widget group leader
  172.         _EXTRA=extra)               ; extra information
  173.  
  174.     ;  Return if data not found (INSGET displays own error messages).
  175.     ;
  176.     if (count ne 1) then $
  177.         RETURN, 0B
  178.  
  179.     ; ---------------------------------------------------------
  180.     ;  Perform biasing.
  181.     ; ---------------------------------------------------------
  182.  
  183.     minVal = MIN(inputData, MAX=maxVal)
  184.     bias = 0.3 * (maxVal - minVal)
  185.     bias = bias * (*psState).biasMult
  186.     newData = inputData + bias
  187.  
  188.     ; ---------------------------------------------------------
  189.     ;  Put the new data into Insight.
  190.     ; ---------------------------------------------------------
  191.  
  192.     description = 'Biased "' + inputName + '" by ' + $
  193.         STRING(STRCOMPRESS(bias, /REMOVE_ALL)) + '.'
  194.     outputName = (*psState).outputName
  195.  
  196.     INSPUT, $
  197.         newData         , $                     ; the data
  198.         DESCRIPTION     = description, $        ; data description
  199.         NAME            = outputName, $         ; try this data name
  200.         NEW_NAME        = outputNameUsed, $     ; the data name actually used
  201.         REPLACE         = 2, $                  ; prompt user if name conflict
  202.         COUNT           = count, $              ; returned # of items put
  203.         CIDs            = CIDs, $               ; command ID list
  204.         GROUP           = wGroup, $             ; widget group leader
  205.         _EXTRA          = extra                 ; extra information
  206.  
  207.     ;  Return if "put" failed.
  208.     ;
  209.     if (count ne 1) then $
  210.         RETURN, 0B
  211.  
  212.     ; ---------------------------------------------------------
  213.     ;  Visualize new data item.
  214.     ; ---------------------------------------------------------
  215.  
  216.     INSVIS, $
  217.         outputNameUsed, $                       ; name of data item
  218.         TYPE            = 'plot', $             ; visualization type
  219.         MODE            = 'new', $              ; insert | new | overlay
  220.         CIDs            = CIDs, $               ; command ID list
  221.         GROUP           = wGroup, $             ; widget group leader
  222.         _EXTRA          = extra                 ; extra information
  223.  
  224.     ; ---------------------------------------------------------
  225.     ;  Successful return.
  226.     ; ---------------------------------------------------------
  227.  
  228.     RETURN, 1B
  229.  
  230. end                 ; ApplyMyBias
  231.  
  232. ; -----------------------------------------------------------------------------
  233. ;
  234. ;   Purpose:  Main entry point for the PlugIn.
  235. ;
  236. pro PromptUserMyBias, $
  237.     GROUP=wGroup, $         ; IN: group leader widget ID
  238.     DATA_NAME=dataName, $   ; IN: name of data selected in visualization window
  239.     _EXTRA=extra            ; IN: various information
  240.  
  241.     ;  Widget state information.
  242.     ;
  243.     common MyBiasCommon, psState
  244.  
  245.     ;  Create modal main base (non-sizable).
  246.     ;
  247.     title = 'Analysis PlugIn Example - My Bias'
  248.     wMainBase = WIDGET_BASE(TITLE=title, GROUP_LEADER=wGroup, $
  249.         /COLUMN, /MODAL, /TLB_FRAME_ATTR)
  250.  
  251.     value = [ $
  252.         'Select vector (1D) data to plot with', $
  253.         'negative, positive, or no 30% bias.' $
  254.         ]
  255.     for i = 0, N_ELEMENTS(value)-1 do $
  256.         void = WIDGET_LABEL(wMainBase, VALUE=value[i])
  257.  
  258.     ; ------------------------------------------
  259.     ;  Create INPUTS widgets.
  260.     ; ------------------------------------------
  261.  
  262.     wInputsBase = WIDGET_BASE(wMainBase, /COLUMN, /FRAME)
  263.  
  264.     void = WIDGET_LABEL(wInputsBase, VALUE='INPUTS')
  265.  
  266.     wInputDataBase = WIDGET_BASE(wInputsBase, /ROW)
  267.     void = WIDGET_LABEL(wInputDataBase, VALUE='Input: ')
  268.     wInputText = WIDGET_TEXT(wInputDataBase, VALUE=dataName, /EDITABLE)
  269.     wInputBrowseButton = WIDGET_BUTTON(wInputDataBase, VALUE=' Browse... ')
  270.  
  271.     wBiasBase = WIDGET_BASE(wInputsBase, /ROW)
  272.     void = WIDGET_LABEL(wBiasBase, VALUE='Bias: ')
  273.     wBiasBgroup = CW_BGROUP(wBiasBase, ['Negative', 'Positive', 'None'], $
  274.         /NO_RELEASE, /ROW, /RETURN_NAME, /EXCLUSIVE, SET_VALUE=1)
  275.     biasMult = 1
  276.  
  277.     ; ------------------------------------------
  278.     ;  Create OUTPUTS widgets.
  279.     ; ------------------------------------------
  280.  
  281.     outputName = 'Bias Output'
  282.  
  283.     wOutputsBase = WIDGET_BASE(wMainBase, /ROW, /FRAME)
  284.     void = WIDGET_LABEL(wOutputsBase, VALUE='                   ')
  285.  
  286.     wOutputsLabelBase = WIDGET_BASE(wOutputsBase, /COLUMN)
  287.     void = WIDGET_LABEL(wOutputsLabelBase, VALUE='OUTPUTS')
  288.     void = WIDGET_LABEL(wOutputsLabelBase, VALUE='Output:  '+outputName)
  289.  
  290.     ; ------------------------------------------
  291.  
  292.     ;  Create OK/Apply/Cancel buttons using special compound widget.
  293.     ;  (Must pass in main modal base, used to set default and cancel buttons.)
  294.     ;
  295.     wOKApplyCancelButtons = CW_INSAPPLY(wMainBase, _EXTRA=extra)
  296.  
  297.     ;  Create dialog state information.
  298.     ;
  299.     sState = { $
  300.         extra: extra, $
  301.         wMainBase: wMainBase, $
  302.         outputName: outputName, $
  303.         wInputText: wInputText, $
  304.         wInputBrowseButton: wInputBrowseButton, $
  305.         wBiasBgroup: wBiasBgroup, $
  306.         biasMult: biasMult, $
  307.         wOKApplyCancelButtons: wOKApplyCancelButtons $
  308.         }
  309.  
  310.     ;  Store the state in a heap variable.
  311.     ;
  312.     psState = PTR_NEW(sState, /NO_COPY)
  313.  
  314.     ;  Realize the dialog box.
  315.     ;
  316.     WIDGET_CONTROL, wMainBase, /REALIZE
  317.  
  318.     ;  Start event loop.
  319.     ;
  320.     XMANAGER, 'PromptUserMyBias', wMainBase, EVENT_HANDLER='HandleEventsMyBias'
  321.  
  322.     ;  Remove widget state info.
  323.     ;
  324.     PTR_FREE, psState
  325.  
  326. end                 ; PromptUserMyBias
  327.  
  328. ; *****************************************************************************
  329. ;       REGISTRATION FUNCTION
  330. ; *****************************************************************************
  331.  
  332. ; -----------------------------------------------------------------------------
  333. ;
  334. ;   Purpose:  Register the Analysis PlugIn.
  335. ;
  336. function MyBias
  337.  
  338.     ;  Return the Analysis PlugIn Registration Structure.
  339.     ;
  340.     RETURN, { $
  341.         type:       'Analysis_PlugIn', $            ; PlugIn type
  342.         title:      'My Bias...', $                 ; PlugIn title
  343.         purpose:    'Do simple biasing.', $         ; PlugIn purpose
  344.         main_proc:  'PromptUserMyBias', $           ; main callback
  345.         apply_func: 'ApplyMyBias', $                ; apply callback
  346.         version:    '5.0', $                        ; IDL version
  347.         revision:   '1.0' $                         ; PlugIn version
  348.         }
  349.  
  350. end                 ; MyBias
  351.  
  352. ; -----------------------------------------------------------------------------
  353.